package com.sale.system.service.impl;

import com.sale.system.domain.SysOfInfo;
import com.sale.system.domain.dto.SysTheDropDto;
import com.sale.system.mapper.SysMidSplitMapper;
import com.sale.system.mapper.SysOfConfigMapper;
import com.sale.system.mapper.SysOfInfoMapper;
import com.sale.system.service.ISysMidSplitService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 流水号获取实现类
 * @Param
 * @Date 22-12-07
 * @Author hl
 **/
@RequiredArgsConstructor
@Service
public class SysMidSplitServiceImpl implements ISysMidSplitService {

    private final SysOfInfoMapper ofInfoMapper;

    private final SysOfConfigMapper sysOfConfigMapper;

    private final SysMidSplitMapper sysMidSplitMapper;


    /**
     * 根据配置生成对应编码
     *
     * @param str1
     * @return String
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String selectSplitStr(String str1){
        List<SysOfInfo> sysList = ofInfoMapper.selectOfInfoAssList(str1);
        StringBuilder code = new StringBuilder();
        StringBuilder code1 = new StringBuilder();
        Date now = new Date();
        for (SysOfInfo sysOfInfo :
                sysList) {
            int met = Integer.parseInt(sysOfInfo.getInfoSegment());
            switch (met) {
                case 1://日期变量
                    //先判断年月日
                    String ty = sysOfConfigMapper.selectOfValueList(sysOfInfo.getInfoVariable());
                    String varBle = sysOfInfo.getInfoVariable();
                    String kl = "";
                    if (ty.equals("year")) {
                        //获取当前年份
                        kl = DateFormatUtils.format(now, "yyyy");
                        code.append(actualYmd(varBle, kl));
                    } else if (ty.equals("month")) {
                        kl = DateFormatUtils.format(now, "MM");
                        int jk2 = Integer.parseInt(kl);
                        kl = String.valueOf(jk2);
                        //获取当前月份实际值
                        code.append(actualYmd(varBle, kl));
                    } else {
                        kl = DateFormatUtils.format(now, "dd");
                        int jk = Integer.parseInt(kl);
                        kl = String.valueOf(jk);
                        //获取当前天实际值
                        code.append(actualYmd(varBle, kl));
                    }
                    break;
                case 2://流水号
                    code1 = code;//获取未加流水号的值
                    String code2 = code1.toString();
                    long ch = sysMidSplitMapper.selectMidOnBy(code2);
                    int leng = sysOfInfo.getInfoTheLength();//长度
                    int type = Integer.parseInt(sysOfInfo.getInfoMethod());//填充方式
                    String zf = sysOfInfo.getInfoMethodCharacter();//填充字符
                    String ch1 = "";
                    int wei = 0;
                    if (ch > 0) {
                        ch += 1;
                        sysMidSplitMapper.updateMidOne(code2);
                        //证明有流水号
                        wei = String.valueOf(ch).length();
                        wei = leng - wei;
                        if (wei > 0) {
                            zf = characterFor(wei, zf);
                        } else {
                            zf = "";
                        }
                        ch1 = actualValue(sysOfInfo.getInfoVariable(), String.valueOf(ch));
                        if (type == 2) {
                            code.append(zf).append(ch1);
                        } else if (type == 3) {
                            code.append(ch1).append(zf);
                        }else{
                            code.append(ch1);
                        }
                    } else {
                        //获取起始流水号
                        int qiShi = sysOfInfo.getInfoTheNumber();
                        //证明没有有流水号 则新增流水
                        sysMidSplitMapper.addMidSplit(code2, qiShi);
                        wei = String.valueOf(qiShi).length();
                        wei = leng - wei;
                        if (wei > 0) {
                            zf = characterFor(wei, zf);
                        } else {
                            zf = "";
                        }
                        ch1 = actualValue(sysOfInfo.getInfoVariable(), String.valueOf(qiShi));
                        if (type == 2) {
                            code.append(zf).append(ch1);
                        } else if (type == 3) {
                            code.append(ch1).append(zf);
                        }
                    }
                    break;
                case 3://文本
                    code.append(sysOfInfo.getInfoVariable());
                    break;
            }
        }
        return code.toString();
    }

    /**
     * 循环增加填充字符
     */
    public String characterFor(int leng, String zf) {
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < leng; i++) {
            str.append(zf);
        }
        return str.toString();
    }

    /**
     * 拼接流水实际值
     */
    public String actualValue(String infoVariable, String str) {//str是流水号
        String val = sysOfConfigMapper.selectOfValueActual(infoVariable, str);
        int[] array = intArray(Integer.valueOf(str));
        List<SysTheDropDto> list = sysOfConfigMapper.selectOfValueKey(infoVariable);
        Map<String, String> actualMap = list.stream().collect(Collectors.toMap(SysTheDropDto::getLabel, SysTheDropDto::getValue));
        StringBuffer sr = new StringBuffer();
        for (int i = 0; i < array.length; i++) {
            sr.append(actualMap.get(String.valueOf(array[i])));
        }
        val = sr.toString();
        return val;
    }

    /**
     * 拼接日期实际值
     */
    public String actualYmd(String infoVariable, String str) {//是日期
        List<SysTheDropDto> list = sysOfConfigMapper.selectOfValueKey(infoVariable);
        Map<String, String> actualYmdMap = list.stream().collect(Collectors.toMap(SysTheDropDto::getLabel, SysTheDropDto::getValue));
        return actualYmdMap.get(str);
    }

    /**
     * 数字转为字符串
     */
    public static int[] intArray(Integer num) {
        String str = num.toString();// 转化为字符串
        int[] intArray = new int[str.length()];// 新建一个数组用来保存num每一位的数字
        for (int i = 0; i < str.length(); i++) {
            // 遍历str将每一位数字添加如intArray
            Character ch = str.charAt(i);
            intArray[i] = Integer.parseInt(ch.toString());
        }
        return intArray;
    }
}
